﻿<?php
class Longadmin_IndexController extends Zend_Controller_Action
{
    protected $_forms;
    protected $_catalogModel;
	
    public function init()
    {

    }

    public function indexAction()
    {
		$form = new Zend_Form();
		$form->setAction('success')
			  ->setMethod('post');
		
		$form->addElement('text', 'username', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                'Alpha',
                array('StringLength', true, array(3, 128))
            ),
            'required'   => true,
            'label'      => 'User Name',
        ));
		
		
        $form->addElement('select', 'title', array(
            'required'   => true,
            'label'      => 'Title',
            'multiOptions' => array('Mr' => 'Mr','Ms' => 'Ms','Miss' => 'Miss','Mrs' => 'Mrs'),
        ));		
		
        $form->addElement('select', 'role', array(
            'required'   => true,
            'label'      => 'Role',
            'multiOptions' => array('Customer' => 'Customer', 'Admin' => 'Admin'),
        ));		
		
		
		$form->addElement('text', 'email', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                array('StringLength', true, array(3, 128)),
                array('EmailAddress'),
            ),
            'required'   => true,
            'label'      => 'Email',
        ));
		
        $form->addElement('password', 'password', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                array('StringLength', true, array(6, 128))
            ),
            'required'   => true,
            'label'      => 'Password',
        ));		
		$form->addElement('submit', 'submit', array(
            'required' => false,
            'ignore'   => true,
            'decorators' => array('ViewHelper',array('HtmlTag', array('tag' => 'dd', 'id' => 'form-submit')))
        ));
		
		$this->view->form = $form;
		$this->_forms = $form;
    }
	
	public function successAction()
	{	
		//echo "sao bua the?";
		//$x = new Longadmin_Model_User;
		$params = array(
						'host' =>'localhost',
						'username' =>'340608',
						'password' =>'20061973',
						'dbname' => '340608'
					);
		$db = Zend_Db::factory('mysqli', $params);
		$table = 'user';
		Zend_Db_Table_Abstract::setDefaultAdapter($db);		
		$data = array('username'=> $_POST['username'],
					'title' => $_POST['title'],
					'email'  => $_POST['email'],		
					'password' => md5($_POST['password']),
					'role' => $_POST['role']	
		);
		$db->insert($table, $data);
		echo "Done :D";
	}
	
	public function adminAction()
	{
		// phan ve Category 
		$db = Zend_Registry::get('db');
		$table = 'category';
		
		$select = $db->select()->from($table);
		$category = $db->query($select)->fetchAll();
		
		//$CategoryModel = new Model_Category();
		//$category = $CategoryModel->listAllCategory();
		$this->view->category = $category;
		
		
		// phan ve Supplier
		$table = 'supplier';
		$form = new Zend_Form();
		$form->setAction('admin-add') 
			 ->setMethod('post');
		$form->addElement('text','s_tenText',array(
            'required'   => true,
            'label'      => 'Tên Nhà Cung Cấp:',
        ));
		$form->addElement('text','s_diachiText',array(
            'required'   => true,
            'label'      => 'Xuất xứ nhà Cung Cấp:',
        ));
		$form->addElement('submit','Add');
		$select = $db->select()->from($table);
		$suppliers = $db->query($select)->fetchAll();
		
		//$CategoryModel = new Model_Category();
		//$category = $CategoryModel->listAllCategory();
		$this->view->suppliers = $suppliers;		
		$this->view->addSuppliersForm = $form;
		
	}
	
	public function adminAddAction()
	{
		
		$db = Zend_Registry::get('db');
		$table = 'supplier';
		$select = $db->select()->from($table)->where('s_ten= ?',$_POST['s_tenText']);
		$search = $db->query($select)->fetchAll();
		if(count($search) ==0){
			$params = array('s_ten'=>$_POST['s_tenText'],
						's_diachi'=> $_POST['s_diachiText']);
			$db->insert($table,$params);
			$this->view->message = 'Hoàn Thành, Đã Tạo Thêm Nhà Cung cấp '.$_POST['s_tenText'];
		}
		else{
			$this->view->message = 'Đã có nhà cung cấp'.$_POST['s_tenText'];
		}
	}
}